<?php
class Item_Model extends CI_Model{
  public function __construct() {
    parent::__construct();
  }
  public function create($param){
    if(isset($param)){
      if($this->db->insert('object',$param)){
        return '{"code": 1, "msg": "Запись успешно добавлена"}';
      }
      else {
        return '{"code": 0, "msg": "Записать не удалось"}';
      }
    }
  }
  public function update($param){
    if(isset($param)){
      $id = $param['id'];
      unset($param['id']);
      $this->db->where('id',$id);
      if($this->db->update('object',$param)){
        return '{"code": 1, "msg": "Запись успешно обновлена"}';
      }
      else{
        return '{"code": 0, "msg": "Обновить не удалось"}';
      }
    }
  }
  public function delete($param){
    if(isset($param)){
      $this->db->where('id',$param['id']);
      if($this->db->delete('object')){
        return '{"code": 1, "msg": "Запись успешно удалена"}';
      }
      else return '{"code": 0, "msg": "Запись невозможно удалить"}';
    }
  }
  public function read($param){
    if(isset($param)){
      $this->db->where('id',$param);
      $query = $this->db->get('object');
      return json_encode($query->result());
    }
    else {
      $this->db->order_by("id", "desc"); 
      $query = $this->db->get('object');
      return json_encode($query->result());
    }
  }
  public function search($param){
    $this->db->select('*');
    $this->db->from('object');
    $this->db->like('name', $param['search']);
    $this->db->order_by("id", "desc"); 
    $query = $this->db->get(0,10);
    return json_encode($query->result());
  }
  public function readWCatId($param){
    if(isset($param)){
      $this->db->select('*');
      $this->db->from('object');
      foreach ($param['data'] as $key=>$data){
        $this->db->or_where('categoryId', $data);
      }
      $this->db->order_by("id", "desc"); 
      $query = $this->db->get(0,10);
      return json_encode($query->result());
    }
  }
  
  public function publishedPost($param){
    if(isset($param)){
      $this->db->where('id',$param);
      $query = $this->db->get('object');
      return json_encode($query->result());
    }
  }
}